package com.example.home.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.home.activity.GoodsListActivity;
import com.example.home.activity.MainActivity;
import com.example.home.activity.R;
import com.example.home.adapter.ClassifyContentAdapter;
import com.example.home.api.ApiService;
import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.ResponseData;
import com.example.home.viewmodel.classify.ClassifyData;
import com.example.home.viewmodel.classify.CurrentCategory;
import com.example.home.viewmodel.classify.CurrentSubCategory;
import com.lzy.okgo.OkGo;

import java.util.List;
import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ClassifyContentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ClassifyContentFragment extends Fragment {


    private CurrentCategory currentCategory;

    private List<CurrentSubCategory> currentSubCategory;

    private ImageView classifyImg;

    private TextView classifyText;

    private GridView classifyCurrentContent;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private GridView classifyRight;

    public ClassifyContentFragment() {
        // Required empty public constructor
    }

    public static ClassifyContentFragment newInstance(String param1, String param2) {
        ClassifyContentFragment fragment = new ClassifyContentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_classify_content, container, false);
        classifyImg = view.findViewById(R.id.classify_right_current_img);
        classifyText = view.findViewById(R.id.classify_right_current_text);
        classifyCurrentContent = view.findViewById(R.id.classify_right_current_main);
        Log.e("TAG", "Loading content for ID: " + mParam1);
        ApiService.GetCurrentClassify(mParam1, new ResponseCallback<ResponseData<ClassifyData>>() {
            @Override
            public void onSuccess(ResponseData<ClassifyData> result) {
                currentCategory = result.getData().getCurrentCategory();
                currentSubCategory = result.getData().getCurrentSubCategory();
                classifyText.setText(currentCategory.getDesc());
                if(!Objects.equals(currentCategory.getDesc(), "数码")) {
                    Glide.with(view.getContext())
                            .load(currentCategory.getPicUrl())
                            .into(classifyImg);
                    classifyCurrentContent.setAdapter(new ClassifyContentAdapter(view.getContext(), currentSubCategory));
                }
                classifyCurrentContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getContext(), GoodsListActivity.class);
                        intent.putExtra("id", id);
                        intent.putExtra("index",position);

                        Intent intent1 = new Intent();
                        ComponentName cpm = new ComponentName(view.getContext(),
                                "com.example.home.activity.MainActivity");
                        intent1.setComponent(cpm);
                        startActivity(intent1);

                        Intent intent2 = new Intent(view.getContext(), MainActivity.class);
                        intent2.setAction("com.example.home.activity.MainActivity");
                        intent2.setType("text/plain");
                        if(intent2.resolveActivity(requireActivity().getPackageManager())!= null){
                            startActivity(intent2);
                        }

                        Log.e("TAG", "onItemClick: "+id );
                        // 可传递基本类型、String、Parcelable对象
                        startActivity(intent);
                    }
                });
            }

        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 取消 Glide 加载
        Glide.with(this).clear(classifyImg);
        // 取消网络请求（假设使用 OkGo）
        OkGo.getInstance().cancelTag(this);
    }


}